package com.example.superyi.myteam;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
//import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.superyi.myteam.R;
import com.example.superyi.myteam.attentionFragment;
import com.example.superyi.myteam.commendFragment;
//import com.example.testapp.myAnswer.MyAnswerFragment;
import com.example.superyi.myteam.myAnswer.MyAnswerFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

//import km.lmy.searchview.SearchView;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ProblemSearchActivity extends AppCompatActivity {


    Toolbar activityToolbar;
    km.lmy.searchview.SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem_search);

        initView();
    }


//    初始化
    private void initView() {

        activityToolbar = findViewById(R.id.activity_toolbar);
        searchView = findViewById(R.id.searchView);

        activityToolbar.setTitle(this.getTitle());
        activityToolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(activityToolbar);


        List<String> historyList = new ArrayList<>();
        historyList.add("Joker");
        historyList.add("Harry");
        historyList.add("Kate");
        historyList.add("Alice");
        //设置全新的历史记录列表
        searchView.setNewHistoryList(historyList);

        //添加一条历史记录
        searchView.addOneHistory("Jenson");

        //设置搜索框默认值
        searchView.setSearchEditText("test string");

        //设置历史记录点击事件
        searchView.setHistoryItemClickListener(new km.lmy.searchview.SearchView.OnHistoryItemClickListener()
        {
            @Override
            public void onClick(String historyStr, int position) {
                Toast.makeText(ProblemSearchActivity.this, historyStr, Toast.LENGTH_SHORT).show();
            }
        });

        //设置软键盘搜索按钮点击事件
        searchView.setOnSearchActionListener(new km.lmy.searchview.SearchView.OnSearchActionListener() {
            @Override
            public void onSearchAction(String searchText) {
                Toast.makeText(ProblemSearchActivity.this, "搜索-->" + searchText, Toast.LENGTH_SHORT).show();
                searchView.addOneHistory(searchText);
            }
        });


        //设置输入文本监听事件
        searchView.setOnInputTextChangeListener(new km.lmy.searchview.SearchView.OnInputTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence charSequence) {
                //TODO something
            }

            @Override
            public void beforeTextChanged(CharSequence charSequence) {
                //TODO something
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //TODO something
            }
        });

//        FloatingActionButton imageButton=findViewById(R.id.imageButton);
//        imageButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(new Intent(ProblemSearchActivity.this,MomentAddActivity.class));
//            }
//        });

        ViewPager2 viewPager2=findViewById(R.id.viewpager2);
        final TabLayout tabLayout=findViewById(R.id.tabLayout2);

        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                switch (position){
                    case 0:
                        return new attentionFragment();
                    case 1:
                        return new commendFragment();
                    default:
                        return new MyAnswerFragment();
                }
            }

            @Override
            public int getItemCount() {
                return 3;
            }
        });

//        绑定
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position){
                    case 0:
                        tab.setText(R.string.attention);
                        break;
                    case 1:
                        tab.setText(R.string.recommend);
                        break;
                    case 2:
                        tab.setText(R.string.answer);
                        break;
                }
            }
        });

        tabLayoutMediator.attach();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);//加载menu文件到布局
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {//自动打开关闭SearchView
            searchView.autoOpenOrClose();
        }
        if (item.getItemId() ==R.id.action_search1){
            startActivity(new Intent(ProblemSearchActivity.this, com.example.superyi.myteam.MomentAddActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }


}
